package scau.mis4.chanyulin.base;

import scau.mis4.chanyulin.utils.BitsUtils;
import scau.mis4.chanyulin.utils.ByteUtils;

/**
 * Created by cyl on 2017/12/9.
 * 表示一个GF(2^8)数
 */
public class GF28 extends Bits {

    public static final GF28 M = new GF28(new byte[]{1, 0, 0, 0, 1, 1, 0, 1, 1});
    public static final GF28 ZERO = new GF28(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
    public static final GF28 ONE = new GF28(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1});

    public GF28(byte[] values) {
        super(9);
        this.setValues(values);
    }

    /**
     * 由字节创建
     * @param b
     */
    public GF28(byte b) {
        super(9);
        byte[] bits = ByteUtils.toBits(b);
        this.set(0, 0);
        for (int i = 0; i < 8; i++) {
            this.set(i + 1, bits[i]);
        }
    }

    /**
     * GF(2^8)数值转为字节
     * @return
     */
    public byte toByte() {
        byte[] newArr = new byte[8];
        byte[] oldArr = this.getValues();
        for (int i = 0; i < 8; i++)
            newArr[i] = oldArr[i + 1];
        return BitsUtils.toByte(newArr);
    }

    public static GF28 getZeroInstance() {
        return new GF28(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
    }

    public static GF28 getOneInstance() {
        return new GF28(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1});
    }

}
